package csu.web.mypetstore.web.servlet;
//登录的逻辑判断
import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

//sessionscope&&requestscope
@WebServlet(name="SignOnServlet",urlPatterns = {"/signOn"})
public class SignOnServlet extends HttpServlet {

    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";
    public String username;
    private String password;

    private String msg;
    //从表单提交，是post请求
    //此URL不支持post方法？
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");

        String piccode = (String) req.getSession().getAttribute("piccode");//验证码内容
        String checkCode = req.getParameter("checkCode2");//用户输入

//        //验证码内容未获得？
//        System.out.println(piccode);
//        System.out.println(checkCode);
        //校验用户输入的正确性
        if (checkCode.equals(piccode)) {
            //验证码输入正确
            if (!validate()) {
                //没有通过校验---回到登陆界面
                req.setAttribute("signOnMsg", this.msg);//显示错误信息
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
            } else {
                //通过校验，
                AccountService accountService = new AccountService();
                Account loginAccount = accountService.getAccount(username, password);
                //判断账号密码是否正确
                if (loginAccount == null) {
                    //账户密码错误
                    System.out.println("error");
                    this.msg = "用户名密码错误";
                    req.setAttribute("signOnMsg", this.msg);//显示错误信息
                    req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);

                } else {
                    //正确
                    //放到session中
                    loginAccount.setPassword(null);
                    HttpSession session = req.getSession();
                    session.setAttribute("loginAccount", loginAccount);

                    session.setAttribute("action","signOn");

                    if (loginAccount.isListOption()) {
                        CatalogService catalogService = new CatalogService();
                        List<Product> myList = catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                        session.setAttribute("myList", myList);

                    }
                    resp.sendRedirect("mainForm");
                }

            }
        }else{
            //验证码输入不正确
            this.msg="验证码输入错误";
            req.setAttribute("signOnMsg", this.msg);//显示错误信息
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);

        }



    }


    //校验用户输入的正确性
    private boolean validate(){
        if(this.username==null||this.username.equals("")){
            this.msg="用户名不能为空";
            return false;
        }
        if(this.password==null||this.password.equals("")){
            this.msg="密码不能为空";
            return false;
        }
        return true;

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

}
